##
## postgresql.py 
##
## Offers postgresql functionality for web2Admin script.
## (ie create database, create user)
##
## Author: Shannon Eric Peevey <speeves@erikin.com>, 7 April 2004
##

import os
from pyPgSQL import PgSQL
import Mysql

class PostgresqlFunc:
    """Offers postgresql functionality for web2Admin script.
    (ie create database, create user)"""

    def AddPostgresqlDatabase(self, config):
        x="y"

        while x=="y":
            database = raw_input("What is the name of the database that you would like to create? ")
            owner = raw_input("Who would you like to be the dbo? ")

            # connect and set conn.autocommit to true, so the CREATE DATABASE query will execute
            # If conn.autocommit is not set, pyPgSQL automagically creates a transaction
            # and we're screwed
            conn = self.Connect(config)
            conn.autocommit=1

            # create cursor
            cursor = conn.cursor()

            # set variables to create database, etc
            cursor.execute("CREATE DATABASE " + database + " WITH OWNER = " + owner + ";")
                
            print "Creating a new Database..."
            x = raw_input("Would you like to create another? (y for yes, n for no) ")

            conn.close()

    def AddPostgresqlUser(self, config, utilityObject):
        """ Get the username for the new user """
        if user == "":
            user = utilityObject.GetUsername(user)

        if password == "":
            password = raw_input("What is the user's password? ")

        x="y"
        while x=="y":
            database = raw_input("What is the name of the database? " )
                
            # connect and set conn.autocommit to true, so the CREATE DATABASE query will execute
            # If conn.autocommit is not set, pyPgSQL automagically creates a transaction
            # and we're screwed
            conn = self.Connect(config)
            conn.autocommit=1

            # create cursor
            cursor = conn.cursor()

            # create user:password in password file
            cursor.execute("CREATE USER " + user + " WITH ENCRYPTED PASSWORD '" + password + "';")

            # check the mysql.newuser.euid table for existence of user
            # add them if they are not there
            checkUser = Mysql.Mysql()
            checkUser.CheckMysqlNewuserTable(config, user)
            del checkUser

            print "Adding user to the Postgresql database..."
            print "\n"

            x = raw_input("Would you like to add another user? (y for yes, n for no) ")
            if x == "y":
                user = raw_input("What is the username? ")
                password = raw_input("What is the password? ")

            conn.close()
                
    def Connect(self, config):
        dsn = config['Postgresql']['Server' + config['Session']['Postgresql']['CurrentServer']]["postgresqlHostname"] + ":" + config['Postgresql']["postgresqlPort"] + ":template1"
        if config['General']["debugLevel"] == '1':
            print dsn
        db = PgSQL.connect(dsn, config['Postgresql']['Server' + config['Session']['Postgresql']['CurrentServer']]["postgresqlUser"], config['Postgresql']["postgresqlUserPassword"])
        return db

